package Java基础.基础语法;

import sun.misc.Cache;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class 不定长单链表 {
    static int mylist[] = new int[20];
    static int  mylistnew = 0;

    /**
     * 进行链表元素添加
     * @param newnum
     * @return
     */
    public  int  addmylist(int newnum){
        if(mylistnew< mylist.length){
            mylist[mylistnew] = newnum;
            mylistnew++;
            return newnum;
        }else {bigmylist(); addmylist(newnum);}
        return 0;
    }

    public int getsize(){
        return mylistnew;
    }

    private  void bigmylist() {
        int[] newlist = new int[(int) (mylist.length*1.1)];
        for (int i = 0;i<mylist.length;i++){
            newlist[i] = mylist[i];
        }
        mylist = newlist;
    }

    public int remove(int x){
        int a = mylist[x];
        if (x == mylistnew){
            mylist[mylistnew] = 0;
            return a;
        }
        for(int i = 0;i<mylist.length;i++){
            if(i == x){
                mylist[i] = mylist[i+1];
                mylistnew--;
            }
        }
        return a;
    }

    /**
     * 查找指定元素的第一个位置
     * @param x
     * @return
     */
    public int find(int x){
        for(int i =0;i<mylist.length;i++){
            if(mylist[i]==x){
                return x;
            }
        }
        System.out.println("找不到");
        return 0;
    }

    /**
     * 替换一个值
     * @param x
     * @param value
     * @return
     */
    public int replace(int x,int value){

        if (x<mylistnew){
            int a = mylist[x];
            mylist[x] = value;
            return a;
        }else{
            return -1;
        }
    }
}
class myexception extends Exception{
    public myexception(String s) {
        System.out.println(s);
    }
}
